package util.concurrent;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.Lombok;
import lombok.SneakyThrows;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.junit.Test;

import java.util.Date;
import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Khai Loon
 * @since 2018/5/17
 */
public class ThreadPool {

    public static void main(String[] args) throws InterruptedException {
        System.out.println("==================start==================");
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(2));
        executor.setRejectedExecutionHandler(new Policy());
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            executor.execute(() -> {
                System.out.println(Thread.currentThread().getName() + "任务i="+ finalI);
                try {
                    TimeUnit.SECONDS.sleep(10000000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

//        executor.shutdown();//只是不能再提交新任务，等待执行的任务不受影响
//
        while (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
            System.out.println("线程池没有关闭 继续等待");
        }

        System.out.println("==================end====================");
//
//
//        executor.execute(() -> {
//            System.out.println(Thread.currentThread().getName() + "再来一次运行");
//        });
    }

    /**
     * guava 构建
     */
    @Test
    public void test1() {
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("guava线程").build();

        ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 200,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(1024), threadFactory, new ThreadPoolExecutor.AbortPolicy());


        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            pool.execute(() -> {
                System.out.println(111);
            });
        }
    }

    public static class Policy implements RejectedExecutionHandler {
        public Policy() { }

        @SneakyThrows
        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            System.out.println("变成阻塞队列");
            e.getQueue().put(r);//异步变成同步
            System.out.println("变成阻塞队列结束");
        }
    }

    @Test
    public void test3() throws InterruptedException {
        ScheduledThreadPoolExecutor poolExecutor = new ScheduledThreadPoolExecutor(1);

        //scheduleAtFixedRate 开始时间内
        //scheduleWithFixedDelay
        System.out.println(DateFormatUtils.format(new Date(),"mm:ss"));
        poolExecutor.schedule(()->{
            System.out.println("开始调度1 "+ DateFormatUtils.format(new Date(),"mm:ss"));
        },2,TimeUnit.SECONDS);


        poolExecutor.schedule(()->{
            System.out.println("开始调度2 "+ DateFormatUtils.format(new Date(),"mm:ss"));
        },10,TimeUnit.SECONDS);

        Thread.currentThread().join();
    }
}
